from typing import Any, Dict

from django.contrib import admin
from django.db import models

from .mixins import PlaceholderMixin

class BaseModel(models.Model):
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    updated_at = models.DateTimeField("更新时间", auto_now=True)

    def update(self, **kwargs: Dict[str, Any]) -> None:
        for k, v in kwargs.items():
            setattr(self, k, v)
        self.save()

    def to_dict(self) -> Dict[str, Any]:
        return {f.name: getattr(self, f.name) for f in self._meta.fields}

    class Meta:
        abstract = True


class BaseAdmin(PlaceholderMixin, admin.ModelAdmin):
    search_placeholder = True
